package com.wei.web.demo.user.filters;

import com.wei.web.demo.user.utils.DemoUtil;
import com.wei.web.middleware.httpfilter.filter.init.FilterInitialize;
import com.wei.web.middleware.httpfilter.registrar.model.FilterConfigDTO;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import java.io.IOException;

@Slf4j
@Component
public class CustomFilter extends OncePerRequestFilter implements FilterInitialize {
    private Integer num = 0;
    @Autowired
    private DemoUtil demoUtil;

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        System.out.println("CustomFilter doFilter" + ++num);
        if (demoUtil != null) {
            System.out.println("demoUtil: " + demoUtil.getConfig());
        }
        filterChain.doFilter(request, response);
    }

    @Override
    public void init(FilterConfigDTO filterConfigDTO) {
        System.out.println(filterConfigDTO.getConfig());
    }

}
